package com.qf.service.impl;/*
 *徐韬
 *2021/11/3
 *17:35
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.channelCache;
import com.qf.constons.resultCode;
import com.qf.events.updateChannelEvents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.queryDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.channelMapper;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TChannel;
import com.qf.service.channelService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Year;
import java.util.List;
@Service
@Transactional
public class channelServiceimpl implements channelService {
    private channelCache channelCache;
    private channelMapper channelMapper;
    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setChannelMapper(com.qf.mapper.channelMapper channelMapper) {
        this.channelMapper = channelMapper;
    }

    @Autowired
    public void setChannelCache(com.qf.cache.channelCache channelCache) {
        this.channelCache = channelCache;
    }

    @Override
    public PageInfo<TChannel> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type != 1){
            throw new queryDataException("您没有权限查看该条数据", resultCode.NO_ROOT);
        }
        List<TChannel> tChannels = channelCache.getfinlAll();
        PageInfo<TChannel> pageInfo = new PageInfo<>(tChannels);
        return pageInfo;
    }

    @Override
    public void addChannel(TChannel tChannel) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser  = (MyAuthUser) principal;
        if(myAuthUser.getType()!=1){
            throw new addDataException("您没有权限对该数据进行添加",resultCode.NO_ROOT);
        }
        channelMapper.addchannel(tChannel);
        context.publishEvent(new updateChannelEvents());

    }

    @Override
    public void updateChannel(TChannel tChannel) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        if(myAuthUser.getType() != 1){
            throw new updateException("你没有权限修改该条数据",resultCode.NO_ROOT);
        }
        int update = channelMapper.update(tChannel);
        context.publishEvent(new updateChannelEvents());
    }

    @Override
    public void deleteByids(List<Integer> ids) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        if(myAuthUser.getType()!=1){
            throw new updateException("你没有权限删除该条数据", resultCode.NO_ROOT);
        }
        int i = channelMapper.deleteByids(ids);
        context.publishEvent(new updateChannelEvents());
    }
}
